package com.leo;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.stage.Stage;

import java.util.Comparator;

/**
 * @ClassName ListPropertyDemo
 * @Description
 * @Author wangss
 * @date 2020.05.28 23:05
 * @Version 1.0
 */
public class ListPropertyDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        ObservableList<String> list = FXCollections.observableArrayList();
        list.add("A");
        list.add("B");
        list.add("C");
        SimpleListProperty<String> listProperty = new SimpleListProperty<>(list);

        /*listProperty.addListener(new ChangeListener<ObservableList<String>>() {
            @Override
            public void changed(ObservableValue<? extends ObservableList<String>> observable, ObservableList<String> oldValue, ObservableList<String> newValue) {
                oldValue.forEach(System.out::println);
                System.out.println("--------------------------------");
                newValue.forEach(System.out::println);
            }
        });*/

        list.addListener(new ListChangeListener<String>() {
            @Override
            public void onChanged(Change<? extends String> c) {
                System.out.println(c);
                c.getList().forEach(System.out::println);

                while (c.next()) {
                    System.out.println("getFrom = " + c.getFrom());
                    System.out.println("getTo = " + c.getTo());
                    System.out.println("wasAdded = " + c.wasAdded());
                    System.out.println("wasRemoved = " + c.wasRemoved());
                    System.out.println("wasReplaced = " + c.wasReplaced());
                    System.out.println("wasPermutated = " + c.wasPermutated());
                    System.out.println("wasUpdated = " + c.wasUpdated());
                }
            }
        });

        list.add("D");

        listProperty.sort(Comparator.reverseOrder());

        listProperty.forEach(System.out::println);


        Platform.exit();
    }
}
